<?php
/**
 * Attachment model 
 *
 * @category Model
 * @package  CriosCMS
 * @version  1.0
 * @author   Lucian SABO <luci@criosweb.ro>
 * @license  http://www.opensource.org/licenses/mit-license.php The MIT License
 * @link     http://cms.criosweb.ro/
 */
class Attachment extends AppModel {
/**
 * Model name
 *
 * @var string
 * @access public
 */
    public $name = 'Attachment';

/**
 * Model associations: belongsTo
 *
 * @var array
 * @access public
 */
    public $belongsTo = array(
        'Node' => array(
            'className' => 'Node',
	    'dependent' => true
        ),
    );
    
/**
 * Uploads directory
 *
 * relative to the webroot.
 *
 * @var string
 * @access public
 */
    public $uploadsDir = 'uploads';    
    
    public $types = array (
	'FILE' => 0,
	'IMAGE' => 1,	
    );

    function delete($id = null, $cascade = true) {
	
	if (! is_numeric ($id)) {
	    return false;
	}
    
	$attachment = $this->findById($id);
	
	if (isset($attachment['Attachment'])) {	    	    
	    if (parent::delete($id, $cascade)) {
		try {
		    $this->deleteAttachment($attachment['Attachment']['filename'], $attachment['Attachment']['type']);
		} catch (Exception $e) {
		    throw new Exception ($e->getMessage());
		}
	    }	    	    
	} else {
	    throw new Exception (__('Could not retrieve the specified attachment', true));
	}
	
	return true;
    }
    
    function deleteAttachment($filename, $type) {
	
	// get folder
	$folder = '';
	switch ($type) {
	    case $this->types['FILE']: $folder = 'files'; break;
	    case $this->types['IMAGE']: $folder = 'images'; break;
	    default: $folder = 'files';
	}	
	
	// delete thumbs
	if ($type == $this->types['IMAGE']) {
	    $thumbs = glob($this->uploadsDir . DS . $folder . DS . 'thumbs' . DS . '*_' . $filename);
	    foreach ($thumbs as $thumb) {		    
		unlink($thumb);
	    }
	}

	$tmp = WWW_ROOT . $this->uploadsDir . DS . $folder . DS . $filename;
	if (file_exists($tmp)) {
	    if (unlink($tmp)) {
		return true;
	    } else {
		throw new Exception (__(sprintf('Failed to delete file %s', $filename), true));
	    }
	}	
    }
    
    /**
     * Returns a list of attachments belonging to the specified node id
     * 
     * @param int $nodeId
     * @param int $type
     * @param int $group
     * @return mixed array of attachments with entries like [id] => filename or false on error
     */
    
    function getByNodeId($nodeId, $type = null, $group = null) {
	
	if (! $nodeId) {
	    return false;
	}		
	
	$conditions = array (
	    'Attachment.node_id' => $nodeId,	    
	);		
	
	if (! is_null($type)) {	    	
	    $conditions[] = array('Attachment.type' => $type);
	}	
	
	if (! empty($group)) {
	    $conditions[] = array('Attachment.group_Id' => $group);
	}
	
	$attachments = $this->find('all', array (
			'fields' => array (
				'Attachment.type',	    
			    'Attachment.filename',
			    'Attachment.title',
			    'Attachment.description',
			),
			'conditions' => array (
			    $conditions
			),
		    )
	    );	

	foreach ($attachments as &$attachment) {
	    switch ($attachment['Attachment']['type']) {
			case $this->types['FILE']: $attachment['Attachment']['filename'] = '/' . $this->uploadsDir . '/files/' . $attachment['Attachment']['filename']; break;
			case $this->types['IMAGE']: $attachment['Attachment']['filename'] = '/' . $this->uploadsDir . '/files/' . $attachment['Attachment']['filename']; break;
	    }
	}
	
	return $attachments;
    }
}
?>